package com.leyou.goods.controller;

import com.leyou.common.bean.Result;
import com.leyou.goods.config.GoodsHtmlGenerator;
import com.leyou.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author zqq
 * @ClassName GoodsController
 * @Description
 * @date 2020/3/16-21:39
 */
@Controller
@RequestMapping("item")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsHtmlGenerator goodsHtmlGenerator;

    /**
     * 跳转到商品详情页
     * @param id
     * @return
     */
    @GetMapping("{id}.html")
    public ModelAndView toItemPage(@PathVariable("id")Long id){
        ModelAndView view = new ModelAndView();
        Result<Map<String, Object>> mapResult = this.goodsService.loadData(id);
        if(mapResult.success()){
            view.setViewName("item");
        }else{
            view.setViewName("error/error-500");
        }
        // 把数据放入数据模型
        view.addObject("result",mapResult);
        //页面静态化
        this.goodsHtmlGenerator.asyncExcute(id);
        return view;
    }
}
